<style>
</style>

<template>
    <div class="table">
        <!-- 头部菜单按钮 -->
        <div class="table-header">
            <Row>
                <Col span="12">
                    <div style="float:left;">
                        <Button type="error" icon="android-remove" @click="multiRemove">批量删除</Button>
                    </div>
                </Col>
                <Col span="12">
                    <div style="float:right;"> 
                        <Input v-model="keywords" placeholder="请输入..." style="width: 150px"></Input>
                        <Button type="ghost" icon="ios-search">搜索</Button>
                    </div>
                </Col>
            </Row>
        </div>
        <Table border :columns="columns" :data="list" @on-selection-change="select"></Table>
        <Page :current.sync="page" :total="total_count" :page-size="limit" @on-change="getList" show-total show-elevator :style="{'text-align': 'center'}"></Page>
    </div>
</template>
<script>
import http from '../../../libs/http';
export default {
    data () {
        return {
            list: [],
            limit: 15,
            page:1,
            total_count: 0,
            keywords: '',
            check_ids: [],
            columns: [
                {
                    type: 'selection',
                    width: 60,
                    align: 'center',
                },
                {
                    title: 'ID',
                    key: 'id',
                    align: 'center'
                },
                {
                    title: '用户名',
                    key: 'nickname',
                    align: 'center'
                },
                {
                    title: '分数',
                    key: 'score',
                    align: 'center'
                },
                {
                    title: '获得学分',
                    key: 'points',
                    align: 'center'
                },
                {
                    title: '考试花费时间',
                    key: 'spend_time',
                    align: 'center'
                },
                {
                    title: '考试时间',
                    key: 'create_time',
                    align: 'center'
                },
                {
                    title: '操作',
                    key: 'action',
                    align: 'center',
                    render: (h, params) => {
                        return h('div', [
                            h('Button', {
                                props: {
                                    type: 'error',
                                    size: 'small'
                                },
                                on: {
                                    click: () => {
                                        this.remove(params.row.id)
                                    }
                                }
                            }, '删除')
                        ])
                    }
                }
            ]
        }
    },
    methods: {
        getList( page ) {
            this.page = page;
            var that = this;
            var url = 'admin/test/record/page/' + page + '/limit/' + that.limit;
            this.apiGet(url).then((res) => {
                if (res.code != 200) {
                    this.handleError(res);
                } else {
                    that.list = res.data.list;
                    that.total_count = res.data.total_count;
                }
            })
        },
        remove(id) {
            this.$Modal.confirm({
                width: "200",
                content: '确认是否删除',
                onOk: () => {
                    this.delete(id);
                }
            });
        },
        delete(id) {
            var url = 'admin/test/record/delete/'
            this.apiDelete(url, id).then((res) => {
                if (res.code != 200) {
                    console.log(res);
                    this.handleError(res);
                } else {
                    this.getList(this.page);
                    this.$Message.success(res.msg);
                    
                }
            });
        },
        multiRemove () {  //批量删除前判断
            var length = this.check_ids.length;
            if (length == 0 ) {
                this.$Message.error('请选择要删除的数据');
            } else {
                this.$Modal.confirm({
                    width: "200",
                    content: '确认是否删除',
                    onOk: () => {
                        this.multiDelete();
                    }
                });
            }
        },
        multiDelete() {   //批量删除
            var url = 'admin/course/record/multiDelete';
            var data = {
                ids: this.check_ids
            }
            this.apiPost(url, data).then((res) => {
                if (res.code != 200) {
                    this.handleError(res);
                } else {
                    this.$Message.success(res.msg);
                    this.getList(this.page);
                }
            })
        },
        select (selection) {  //获取选中的对象
            var obj = [];
            selection.forEach(function(val, index) {
                obj.push(val.id);
            }, this);
            this.check_ids = obj;
            console.log(obj);
        },
    },
    created () {
        this.getList(this.page);
    },
    mixins:[http]
}
</script>
